package com.sf.osms.dep.processor.nio;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.sf.osms.dep.processor.util.DateUtil;

public class CsvRowDecoder implements Decoder<String[]>{
	
	 public static final String DEFAULT_ENCODING = "UTF-8";
	 private static final byte LF = 10;
	 private static final byte RT = 13;
	 private final byte delimiter;
	 private String encoding = DEFAULT_ENCODING;
	 
	 public CsvRowDecoder(byte delimiter,String encoding) {
		   this.delimiter = delimiter;
		   if(!StringUtils.isBlank(encoding)){
			   this.encoding = encoding;
		   }
		  
	 }
	@Override
	public String[] decode(ByteBuffer buffer) {
		
		 	int lineStartPos = buffer.position();
		    int limit = buffer.limit();
		    while (buffer.hasRemaining()) {
		      byte b = buffer.get();
		      if (b == LF) { // reached line feed so parse line
		    	
		        int lineEndPos = buffer.position();
		        // set positions for one row duplication
		        if (buffer.limit() < lineEndPos + 1) {
		          buffer.position(lineStartPos).limit(lineEndPos);
		        } else {
		          buffer.position(lineStartPos).limit(lineEndPos + 1);
		        }
		        String[] entry = parseRow(buffer.duplicate());
		        if (entry != null) {
		          // reset main buffer
		          buffer.position(lineEndPos);
		          buffer.limit(limit);
		          // set start after LF
		          lineStartPos = lineEndPos;
		        }
		        return entry;
		      }
		    }
		    buffer.position(lineStartPos);
		    return null;
	}
	
	
	 public String[] parseRow(ByteBuffer buffer) {
		    int fieldStartPos = buffer.position();
		    int fieldEndPos = 0;
		    int fieldNumber = 0;
		    List<String> fields = new ArrayList<String>();
		    while (buffer.hasRemaining()) {
		      byte b = buffer.get();
		      if (b == delimiter || b == LF) {
		    	
		        fieldEndPos = buffer.position();
		        // save limit
		        int limit = buffer.limit();
		        // set positions for one row duplication
		        if(buffer.get(fieldEndPos -2) == RT){
		        	buffer.position(fieldStartPos).limit(fieldEndPos-1);
		        	fields.add(parseField(buffer.duplicate(), fieldNumber, fieldEndPos - fieldStartPos - 2)) ;
		        }else{
		        	buffer.position(fieldStartPos).limit(fieldEndPos);
		        	fields.add(parseField(buffer.duplicate(), fieldNumber, fieldEndPos - fieldStartPos - 1));
		        }
		        fieldNumber++;
		        // reset main buffer
		        buffer.limit(limit);
		        buffer.position(fieldEndPos);
		        // set start after LF
		        fieldStartPos = fieldEndPos;
		      }
		      
		    }
		    
		    return fields.toArray(new String[]{});
		  }
		  
	 private String parseField(ByteBuffer buffer, int pos, int length) {
		    byte[] field = new byte[length];
		    for (int i = 0; i < field.length; i++) {
		      field[i] = buffer.get();
		    }
		    String ret = null;
			try {
				ret = new String(field,encoding);
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			}
		    return ret;
	 }
	 
	  public static void main(String[] args) {
		  File file = new File("E:/第六次10万/第六次10万/dd.txt");
		 
		  char delimiter = '~';
		  CsvRowDecoder cd = new CsvRowDecoder((byte) delimiter,null);
		  FileReader<String[]> fr = FileReader.create(cd, file);
		  Iterator<List<String[]>> iterator = fr.iterator();
		  int size = 0;
		  System.out.println(DateUtil.dateFomat(new Date(), "yyyy-MM-dd HH:mm:ss"));
		  while (iterator.hasNext()) {
			List<String[]> next = iterator.next();
			size = size + next.size();
			System.out.println(next);
		}
	
		System.out.println(file.delete());
		 
	
		  }  
	  

}
